package com.atguigu.exam.service.impl;


import com.atguigu.exam.controller.QuestionController;
import com.atguigu.exam.entity.Question;
import com.atguigu.exam.entity.QuestionAnswer;
import com.atguigu.exam.entity.QuestionChoice;
import com.atguigu.exam.mapper.QuestionAnswerMapper;
import com.atguigu.exam.mapper.QuestionChoiceMapper;
import com.atguigu.exam.mapper.QuestionMapper;
import com.atguigu.exam.service.QuestionService;
import com.atguigu.exam.vo.QuestionPageVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 题目Service实现类
 * 实现题目相关的业务逻辑
 */
@Slf4j
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private QuestionAnswerMapper questionAnswerMapper;

    @Autowired
    private QuestionChoiceMapper questionChoiceMapper;

    @Override
    public void queryPage(Page<Question> questionPage, QuestionPageVo questionQueryVo) {
        questionMapper.customPage(questionPage, questionQueryVo);
    }

    @Override
    public void customPageJavaService(Page<Question> pageBean, QuestionPageVo questionPageVo) {
        //分页查询题目列表（多条件）
        LambdaQueryWrapper<Question> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(questionPageVo.getType()), Question::getType, questionPageVo.getType());
        lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(questionPageVo.getDifficulty()), Question::getDifficulty, questionPageVo.getDifficulty());
        lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(questionPageVo.getCategoryId()), Question::getCategoryId, questionPageVo.getCategoryId());
        lambdaQueryWrapper.like(!ObjectUtils.isEmpty(questionPageVo.getKeyword()), Question::getTitle, questionPageVo.getKeyword()).
                like(!ObjectUtils.isEmpty(questionPageVo.getKeyword()), Question::getAnalysis, questionPageVo.getKeyword());
        page(pageBean, lambdaQueryWrapper);
        //提取一个方法，给题目进行选项和答案
        fillQuestionChoiceAndAnswer(pageBean.getRecords());

    }

    private void fillQuestionChoiceAndAnswer(List<Question> questionList) {
        //1 非空判断
        if (questionList == null || questionList.size() == 0) {
            log.debug("没有查询对应的问题集合数据!!");
            return;
        }
        //2 查询所有答案和选项
        List<Long> ids = questionList.stream().map(Question::getId).collect(Collectors.toList());
        //查询本次题目的选项集合
        List<QuestionChoice> questionChoiceList = questionChoiceMapper.selectList(
                new LambdaQueryWrapper<QuestionChoice>()
                        .in(QuestionChoice::getQuestionId, ids)
        );
        // 查询本次题目的答案集合
        List<QuestionAnswer> questionAnswers  = questionAnswerMapper.selectList(
                new LambdaQueryWrapper<QuestionAnswer>()
                        .in(QuestionAnswer::getQuestionId, ids)
        );

 //答案和选项进行map 转换
        Map<Long, List<QuestionChoice>> questionChoiceMap =
                questionChoiceList.stream().collect(Collectors
                        .groupingBy(QuestionChoice::getQuestionId));

        Map<Long, QuestionAnswer> answerMap =
                questionAnswers.stream().collect(Collectors
                        .toMap(QuestionAnswer::getQuestionId, a -> a));
//循环问题集合，给问题设置选项和答案
        questionList.forEach(question -> {
            question.setAnswer(answerMap.get(question.getId()));
            if ("CHOICE".equals(question.getType())){
                List<QuestionChoice> questionChoices = questionChoiceMap.get(question.getId());
                questionChoices.sort(Comparator.comparingInt(QuestionChoice::getSort));
                question.setChoices(questionChoices);
            }
        });
    }
}